
<script type="text/javascript">
    $(document).ready(function() {
        $('#send-results').button();
        $('#send-results').click(function () {
            $('<div><p>Introduce tu e-mail para que podamos ponernos en contacto contigo:</p><input type="text" id="email" /></div>').dialog({
                modal: true,
                buttons: {
                    Enviar: function() {
                        $.ajax({
                            type: 'POST',
                            //processData: false,
                            dataType: 'html',
                            url: '<? echo $this->baseUrl() ?>/user-tests/send-results',
                            data: ({user: $('#email').val(), content: $('#questions').html()}),
                            success: function(data) {
                                if (data.substring(data.length-4, data.length) == "\"ok\"") {
                                    window.alert("El e-mail con los resultados ha sido enviado correctamente.")
                                } else  {
                                    window.alert("Se ha producido un error al intentar enviar el e-mail con los resultados.")
                                }
                            }
                        });
                        $( this ).dialog( "close" );
                    },
                    Cancel: function() {
                        $( this ).dialog( "close" );
                    }
                }

            });
            /*$.ajax({
                url: '<? echo $this->baseUrl() ?>/user-tests/send-results'
            });
            window.alert('R');*/
        });
    });

    function viewFails(test_id, questionnaire_id, name_q) {
        var url = "<? echo $this->baseUrl() ?>/user-tests/view-fails/?test_id="+test_id+"&questionnaire_id="+questionnaire_id;
        $.ajax({
            type: 'GET',
            url: url,
            success: function(data) {
                var results = $("<div></div>").html(data);
                results.appendTo('#results');

            }

        });

    }
</script>

<div id="section-content">

    <h2>Test results: <? echo $this->test->getName(); ?> (<? echo $this->test->getLevelName(); ?>)</h2>


    <div id="questionnaire-list" class="ui-widget-header ui-corner-top">
        <ul id="test-questionnaires">
            <?php foreach($this->questionnaires as $q):  ?>

            <li>
                    <? echo $q->getName(); ?>
                &gt;
            </li>
            <? endforeach; ?>
            <li class="selected-questionnaire">
                Results
            </li>
        </ul>
        <div style="clear:both;"></div>
    </div>

    <div id="questions">
        <h4 style="margin-left: 25px;">Preguntas acertadas <span id="result-stats"><? echo $this->all_ok."/".($this->all_fail+$this->all_ok); ?></span></h4>
        <h4 style="margin-left: 25px;">Tu nivel parece ser <span id="result-level"><? echo $this->test->getLevelName(); ?></span></h4>
        <ul>
            <?

            foreach ($this->results as $name_q => $results_q) {
                echo "<li>";
                echo $name_q.": ";
                if ($results_q['n_fails'] == 0) {
                    echo "<span class='zero_fails'>";
                } else {
                    echo "<span class='n_fails'>";
                }
                echo $results_q['n_fails'];
                if ($results_q['n_fails'] == 1) {
                    echo " fail. ";
                } else {
                    echo " fails. ";
                }

                echo "</span>";

                echo " (".$results_q['tries'];
                if ($results_q['tries'] == 1) {
                    echo " attempt)";
                } else {
                    echo " attempts)";
                }

                if ($results_q['n_fails'] > 0) {
                    ?>
            <script type="text/javascript">
                $(document).ready(function(){
                    viewFails(<?php echo $this->escape($this->test->id) ?>, <?php echo $results_q["questionnaire_id"] ?>, '<? echo $name_q ?>');
                });
            </script>

                <?
                }
                echo "</li>";

            }
            ?>
        </ul>
        <div id="results">

        </div>

        <?
        /*foreach($this->session as $key => $value) {
            echo $key."<br />";
            print_r($value);
            echo "<br /><br />";
        }*/
        ?>
    </div>
    <div id="send-results">
        <a href="#" id="send-results">Solicitar más información y enviar los resultados a la academia.</a>
    </div>
</div>